package chabernac.android.wordbattle.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

public class WordTest extends TestCase {
  public void testEquals(){
    Word theWord = createWord("Nora");
    Word theWord2 = createWord("Nora");
    assertEquals(theWord, theWord2);
  }
  
  public void testSetContainsWord(){
    Set<Word> theWords = new HashSet<Word>();
    theWords.add(createWord("Nora"));
    assertTrue(theWords.contains(createWord("Nora")));
  }

  private Word createWord(String aWord){
    List<Letter> theLetters = new ArrayList<Letter>();
    char[] theChars = aWord.toCharArray();
    for(char theChar : theChars){
      theLetters.add(new Letter(theChar, 1, 1));
    }
    
    return new Word(theLetters);
  }
  
  public void testLettersNotOnline(){
    List<Letter> theLetters = new ArrayList<Letter>();
    theLetters.add(new Letter('A', 1, 1));
    theLetters.add(new Letter('B', 2, 1));
    theLetters.add(new Letter('C', 3, 1));
    theLetters.add(new Letter('D', 3, 2));
    theLetters.add(new Letter('E', 3, 3));
    theLetters.add(new Letter('F', 4, 3));
    theLetters.add(new Letter('F', 5, 4));

    Word theWord = new Word(theLetters);
    assertFalse(theWord.isLettersOnLine());
  }
  
  public void testLettersOnline(){
    List<Letter> theLetters = new ArrayList<Letter>();
    theLetters.add(new Letter('A', 1, 1));
    theLetters.add(new Letter('B', 2, 1));
    theLetters.add(new Letter('C', 3, 1));
    theLetters.add(new Letter('D', 4, 1));
    theLetters.add(new Letter('E', 5, 1));
    theLetters.add(new Letter('F', 6, 1));
    theLetters.add(new Letter('F', 7, 1));
    
    Word theWord = new Word(theLetters);
    assertTrue(theWord.isLettersOnLine());
  }
  
  public void testEndPointLetters(){
    List<Letter> theLetters = new ArrayList<Letter>();
    //ABC
    //  D
    //  EF
    //    G
    //    
    
    theLetters.add(new Letter('A', 1, 1));  //endpoint letter
    theLetters.add(new Letter('B', 2, 1));
    theLetters.add(new Letter('C', 3, 1));  //endpoint letter
    theLetters.add(new Letter('D', 3, 2));
    theLetters.add(new Letter('E', 3, 3));  //endpoint letter
    theLetters.add(new Letter('F', 4, 3));  //endpoint letter
    theLetters.add(new Letter('G', 5, 4));  //endpoint letter
    
    Word theWord = new Word(theLetters);
    List<Letter> theEndPointLetters = theWord.getEndPointLetters();
    
    assertEquals(5, theEndPointLetters.size());
    assertEquals('A', theEndPointLetters.get(0).getChar());
    assertEquals('C', theEndPointLetters.get(1).getChar());
    assertEquals('E',  theEndPointLetters.get(2).getChar());
    assertEquals('F',  theEndPointLetters.get(3).getChar());
    assertEquals('G',  theEndPointLetters.get(4).getChar());
  }
  
  public void testEndPointLetters2(){
    List<Letter> theLetters = new ArrayList<Letter>();
    //AB
    // CD
    
    theLetters.add(new Letter('A', 1, 1));  //endpoint letter
    theLetters.add(new Letter('B', 2, 1));  //endpoint letter
    theLetters.add(new Letter('C', 2, 2));  //endpoint letter
    theLetters.add(new Letter('D', 3, 2));  //endpoint letter
    
    Word theWord = new Word(theLetters);
    List<Letter> theEndPointLetters = theWord.getEndPointLetters();
    
    assertEquals(4, theEndPointLetters.size());
    assertEquals('A', theEndPointLetters.get(0).getChar());
    assertEquals('B', theEndPointLetters.get(1).getChar());
    assertEquals('C',  theEndPointLetters.get(2).getChar());
    assertEquals('D',  theEndPointLetters.get(3).getChar());
  }
}
